const header = {
	"Content-Type": "application/json",
	"Authorization": "Bearer 878a62d9-286a-4550-bad2-fad2bd9426bc"
}

const data = {
	model: "bot-20241121152454-npzrt",
	messages: []
}

const header1 = {
	"Authorization": "Bearer;GHetYWOD2iCOZU_KEtN2txEi7BM8k7TF"
}

const data1 = {
    app: {
        appid: "6897754890",
        token: "GHetYWOD2iCOZU_KEtN2txEi7BM8k7TF",
        cluster: "volcano_tts"
    },
    user: {
        uid: "uid123"
    },
    audio: {
        voice_type: "BV700_streaming",
        encoding: "mp3",
        compression_rate: 1,
        rate: 24000,
        speed_ratio: 1.0,
        volume_ratio: 1.0,
        pitch_ratio: 1.0,
        language: "cn"
    },
    request: {
        reqid: "uuid",
        text: "",
        text_type: "plain",
        operation: "query",
        silence_duration: "125",
        with_frontend: "1",
        frontend_type: "unitTson",
        pure_english_opt: "1"
    }
}

async function douAgentText(msg) {
	return new Promise((resolve, reject) => {
		if (msg) {
			data.messages.push({
				role: "user",
				content: msg
			})
		}
		uni.request({
			url: "https://ark.cn-beijing.volces.com/api/v3/bots/chat/completions",
			method: 'POST',
			header: header,
			data: data,
			dataType: 'json',
			success: (response) => {
				if (response.data.choices) {
					
					if (response.data.choices[0].finish_reason == 'stop') {
						data.messages.push(response.data.choices[0].message)
					}
					resolve(response.data)
				} else {
					resolve(response.data)
					data.messages.pop()
				}
			},
			fail: (error) => {
				reject(error)
				console.error(error); // 请求失败的情况  
			}
		});
	})
}

// 发起短文本语音合成任务
async function douTextToVoice(msg,type) {
	return new Promise((resolve, reject) => {
		data1.request.text = msg
		if(type == 2){
			data1.audio.voice_type = 'BV158_streaming'
		}else{
			data1.audio.voice_type = 'BV700_streaming'
		}
		uni.request({
			url: `https://openspeech.bytedance.com/api/v1/tts`,
			method: 'POST',
			header: header1,
			data: data1,
			dataType: 'json',
			success: (response) => {
				// console.log("response", response)
				resolve(response.data);
			},
			fail: (error) => {
				console.error(error); // 请求失败的情况  
			}
		});
	});
}




export {
	douAgentText,
	douTextToVoice
}